package com.orange.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orange.commonutils.jwt.JwtUtils;
import com.orange.commonutils.result.Result;
import com.orange.commonutils.vo.UcenterMemberVo;
import com.orange.eduservice.client.UcenterClient;
import com.orange.eduservice.entity.EduComment;
import com.orange.eduservice.service.EduCommentService;
import com.orange.servicebase.exception.MyException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author orange
 * @since 2021-12-27
 */
@RestController
@CrossOrigin
@RequestMapping("/eduService/edu-comment")
public class EduCommentController {

    @Autowired
    private EduCommentService eduCommentService;

    @Autowired
    private UcenterClient ucenterClient;

    //根据课程id分页查询课程评论
    @GetMapping("/getCommentPage/{page}/{limit}/{courseId}")
    public Result getCommentPage(@PathVariable("page") long page,
                                 @PathVariable("limit") long limit,
                                 @PathVariable("courseId") String courseId) {
        //分页对象
        Page<EduComment> commentPage = new Page<>(page, limit);
        //条件查询对象
        QueryWrapper<EduComment> queryWrapper = new QueryWrapper<>();
        //判断课程id是否为空
        if (StringUtils.isNotEmpty(courseId)) {
            queryWrapper.eq("course_id", courseId);
        }
        //评论按发表时间排序
        queryWrapper.orderByAsc("gmt_create");
        //分页查询
        eduCommentService.page(commentPage, queryWrapper);

        List<EduComment> records = commentPage.getRecords();//查询记录
        long total = commentPage.getTotal();    //总记录数
        long pages = commentPage.getPages();    //总页数
        long current = commentPage.getCurrent();//当前页码
        boolean hasPrevious = commentPage.hasPrevious();//是否有上一页
        boolean hasNext = commentPage.hasNext();    //是否有下一页

        HashMap<String, Object> map = new HashMap<>();
        map.put("records", records);
        map.put("total", total);
        map.put("pages", pages);
        map.put("current", current);
        map.put("hasPrevious", hasPrevious);
        map.put("hasNext", hasNext);

        return Result.ok().data(map);
    }

    //添加评论
    @PostMapping("/addComment")
    public Result addComment(HttpServletRequest request, @RequestBody EduComment eduComment) {
        //JWT工具类获取用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //判断用户是否登录
        if (StringUtils.isEmpty(memberId)) {
            throw new MyException(20001, "您还未登录");
        }
        eduComment.setMemberId(memberId);
        //远程调用，获取用户呢称、头像
        UcenterMemberVo memberVo = ucenterClient.getUserInfoById(memberId);
        eduComment.setAvatar(memberVo.getAvatar());
        eduComment.setNickname(memberVo.getNickname());
        //保存评论
        eduCommentService.save(eduComment);
        return Result.ok();
    }
}

